import axios from 'axios'
import Vue from 'vue'
import { Message } from 'element-ui'
import vm from './main'
const service = axios.create({
  baseURL: '/',
  timeout: 20 * 1000,
  headers: {
    // 'Content-Type': 'application/x-www-form-urlencoded'
    'Content-Type': 'application/json;charset=utf-8'
  }
})
service.interceptors.request.use((config) => {
  // 是否需要设置 token
  const isToken = (config.headers || {}).isToken === false
  if (localStorage.getItem('token') && !isToken) {
    config.headers['token'] =
      localStorage.getItem('token')
    config.headers['schoolid'] = '11'
    // 让每个请求携带自定义token 请根据实际情况自行修改

    /*   config.headers["token"] = localStorage.getItem("token") // 让每个请求携带自定义token 请根据实际情况自行修改 */
  }
  if (config.method === 'get' && config.params) {
    let url = config.url + '?'
    for (const propName of Object.keys(config.params)) {
      const value = config.params[propName]
      var part = encodeURIComponent(propName) + '='
      if (value !== null && typeof value !== 'undefined') {
        if (typeof value === 'object') {
          for (const key of Object.keys(value)) {
            let params = propName + '[' + key + ']'
            var subPart = encodeURIComponent(params) + '='
            url += subPart + encodeURIComponent(value[key]) + '&'
          }
        } else {
          url += part + encodeURIComponent(value) + '&'
        }
      }
    }
    url = url.slice(0, -1)
    config.params = {}
    config.url = url
  } else if (config.url !== '/user/login' && config.url !== '/user/reg' && !localStorage.getItem('token')) {
    Message({
      message: '登录状态已过期，请重新登录',
      type: 'warning',
      duration: 3 * 1000
    })
    vm.$router.push({
      path: "/"
    })
  }
  return config
})

// response拦截器
service.interceptors.response.use((response) => {
  if (response.status === 500) {
  }

  const res = response.data
  console.log(res)
  if (res.code === 0) {
    return res
  } else if (res.code === 20000) {
    console.log(response)
    Message({
      message: res.msg,
      type: 'success',
      duration: 3 * 1000
    })
    return res
  } else if (res.data.code === 400) {

    return res.data
  } else if (res.data.code === 500) {
    Message({
      message: res.data.msg,
      type: 'error',
      duration: 3 * 1000
    })
    return res.data
  } else if (!res.data.code) {
    return res.data
  } else if (res.data.code === 998) {
    return res
  } else if (res.data.code === 999) {
    Message({
      message: '请登录',
      type: 'warning',
      duration: 3 * 1000
    })

    return res
  } else if (res.data.code == 401) {
    Message({
      message: '登录状态已过期，请重新登录',
      type: 'warning',
      duration: 3 * 1000
    })
  } else if (!res) {

  } else {
    Message({
      message: '登录状态已过期，请重新登录',
      type: 'warning',
      duration: 3 * 1000
    })
  }
  //  window.location.reload();
  // } else {
  //   endLoading();
  //   Message({
  //     message: "连接超时",
  //     type: "error",
  //     duration: 3 * 1000,
  //   });
  //   return Promise.reject(new Error(res && res.msg ? res.msg : "Error"));
  //   // return Promise.reject(res);
  //   // return res;
  // }
})
export { service }
